package com.gbei.center.oauth.controller.aopController;

import com.gbei.center.base.BaseController;
import com.gbei.center.config.restauth.RestAuthConfig;
import com.gbei.center.oauth.req.ReqUserToken;
import com.gbei.center.oauth.service.ApiService;
import com.gbei.center.req.open.ReqAreaCode;
import com.gbei.center.req.open.ReqThirdpartyPlaceOrder;
import com.gbei.center.req.open.ReqUserCommon;
import com.gbei.center.utils.JsonUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 通过Access Token访问的API服务
 *
 */
@RestController
@RequestMapping("platform")
@Api(description = "提供外部接口")
@RestAuthConfig(release = true) //全局放行
public class PlatformController extends BaseController{
    private static Logger log = LoggerFactory.getLogger(PlatformController.class);

    @Autowired
    private ApiService apiService;

    /**
     * 用户根据openId获取京贝尔系统的微信token
     * @param req
     * @return
     */
    @ApiOperation(value = "用户根据openId获取京贝尔系统的微信token")
    @PostMapping(value = "wx/getAccessToken", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getAccessToken( ReqUserToken req){
        log.info("@@@@@@@@@@@ 第三方用户根据openId获取京贝尔系统的微信token：{}", JsonUtils.toJson(req));
        String apiUrl = "platform/wx/getAccessToken";
        return apiService.getUserToken(req,apiUrl);
    }



    /**
     * 根据token获取用户信息
     * @param
     * @return
     */
    @ApiOperation(value = "根据token获取用户信息")
    @PostMapping(value = "user/getInfo", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getUserInfo(@Valid ReqUserCommon req, BindingResult br){
        log.info("@@@@@@@@@@@ 第三方根据token获取用户信息接口：{}", JsonUtils.toJson(req));
        checkParams(br);
        return apiService.getUserInfo(req);
    }


    @ApiOperation(value = "开放平台注册用户", response = String.class)
    @PostMapping("user/userCommon")
    public String userCommon(@Valid ReqUserCommon req, BindingResult br) {
        log.info("@@@@@@@@@@@ 第三方开放平台注册用户：{}", JsonUtils.toJson(req));
        checkParams(br);
        String apiUrl = "platform/user/userCommon";
       return apiService.userCommon(req,apiUrl);
    }



    @ApiOperation(value = "下单/修改/查询/退款")
    @PostMapping("order/orderCommon")
    public String placeOrder(@Valid ReqThirdpartyPlaceOrder req, BindingResult br) throws Exception {
        log.info("@@@@@@@@@@@ 第三方下单/修改/查询/退款接口：{}", JsonUtils.toJson(req));
        checkParams(br);
        String apiUrl = "platform/order/orderCommon";
        return apiService.placeOrder(req,apiUrl);

    }



    @ApiOperation(value = "获取行政区域编码")
    @PostMapping("area/areaCommon")
    public String getAreaCode(@Valid ReqAreaCode req, BindingResult br) throws Exception {
        log.info("@@@@@@@@@@@ 第三方获取行政区域编码接口：{}", JsonUtils.toJson(req));
        checkParams(br);
        String apiUrl = "platform/area/areaCommon";
        return apiService.getAreaCode(req,apiUrl);
    }



}
